package com.manuserve.mountainduck.kernel;

import android.graphics.*;

import java.util.List;

/**
 * Toutes les fonctions graphiques
 * Created by xmanoux on 09/03/14.
 */
public class Graphics {

    public int width = 0 ;
    public int height = 0 ;
    private Paint paint = new Paint();
    private Canvas canvasCurrent = null ;

    public Graphics() {

    }

    public void setCanvas(Canvas canvas,float cwidth,float cheight) {
        canvasCurrent = canvas ;
        width=(int) cwidth;
        height=(int) cheight;

    }

    public void drawGradientTwoColors(float xstart,float ystart,float xend,float yend,int colorstart,int colorend) {
        setGradientTwoColors( xstart, ystart,xend,yend,colorstart,colorend);
        canvasCurrent.drawRect(new RectF(0,0,xend-xstart,yend-ystart),paint);
    }

    public void setGradientTwoColors(float xstart,float ystart,float xend,float yend,int colorstart,int colorend) {
        this.resetPaint();
        paint.setAlpha(255);
        paint.setShader(new LinearGradient( xstart, ystart, xend-xstart,yend-ystart,colorstart,
                colorend, Shader.TileMode.CLAMP));
        paint.setDither(true);
    }

    public void vignette(float xstart,float cwidth,float variation) {
        this.resetPaint();
        paint.setAlpha(255);
        float scaleY = ( (float) height)/( (float) width) ;
        int[] colors = new int[] { 0, 0, 0x4f000000  };
        float[] pos = new float[] { 0.0f, 0.7f, 1.0f };
        RadialGradient vignette = new RadialGradient (xstart+cwidth/2,
                height/(2*scaleY) ,
                (float) ( (1+variation/10)*cwidth/1.5),
                colors,
                pos,  Shader.TileMode.CLAMP);
        Matrix oval = new Matrix ();
        oval.setScale (1,scaleY);
        vignette.setLocalMatrix(oval);
        paint.setShader(vignette);
        paint.setDither(true);
        canvasCurrent.drawRect(new RectF(xstart,0,xstart+cwidth,height),paint);
        drawRect(0,0,xstart,height,Color.BLACK,1);
        drawRect(xstart+cwidth,0,(width-xstart-cwidth),height,Color.BLACK,1);
    }

    public void drawRect(float x,float y,float cwidth,float cheight,int colorIn,float alpha) {
        this.resetPaint();
        paint.setColor(colorIn);
        this.drawRectFromCompiledPaint(x, y, cwidth, cheight, alpha);
    }

    public void drawRectFromCompiledPaint(float x,float y,float cwidth,float cheight,float alpha) {
        paint.setAlpha((int) (255*alpha));
        paint.setStrokeWidth(0);
        canvasCurrent.drawRect(new RectF(x,y,x+cwidth,y+cheight), paint);
    }


/*
    public void drawQuadratic(float x1,float y1,float x2,float y2,float x3,float y3,int color,float lineThiness,float alpha) {
        paint.reset();
        paint.setColor(color);
        this.drawQuadraticFromCompiledPaint(x1,x2,y1,y2,x3,y3,lineThiness,alpha);
    }*/

    public void drawQuadraticFromCompiledPaint(float x1,float y1,float x2,float y2,float x3,float y3 ,float lineThiness,float alpha) {
        paint.setStrokeWidth(lineThiness);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAlpha((int) (255*alpha));
        Path path = new Path();
        path.moveTo(x1,y1);
        path.quadTo(x2,y2,x3,y3);
        canvasCurrent.drawPath(path,paint);
    }


    public void drawSegments(List<PointF> tabCoords,int color,float thiness,float alpha) {
        this.resetPaint();
        paint.setColor(color);
        paint.setAlpha((int) (255*alpha));
        paint.setStrokeWidth(thiness);
        paint.setStyle(Paint.Style.STROKE);
        Path path = new Path();
        path.moveTo(tabCoords.get(0).x,tabCoords.get(0).y);
        for(int i = 1 ; i < tabCoords.size() ; i++) {
            PointF point = tabCoords.get(i);
            path.lineTo(point.x,point.y);
        }
        canvasCurrent.drawPath(path,paint);
    }


    public void drawSprite(Sprite sprite,float x,float y) {
        this.resetPaint();
        canvasCurrent.drawBitmap(sprite.bmp,x,y,paint);
    }

    public void resetPaint() {
        paint.reset();
        paint.setShader(null);
        paint.setAlpha(255);
    }


    public void drawText(String text,float x,float y,float textSize,int colorIn,int colorOut,float lineOutThiness,float alpha) {
        this.resetPaint();
        paint.setShader(null);
        paint.setColor(colorOut);
        paint.setAlpha((int) (255*alpha));
        paint.setStrokeWidth(lineOutThiness);
        paint.setTextSize(textSize);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvasCurrent.drawText(text, x, y, paint);
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(colorIn);
        paint.setAlpha((int) (255*alpha));
        paint.setStrokeWidth(0);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(textSize);
        canvasCurrent.drawText(text, x, y, paint);
    }


}
